﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SistemaAcademicoBLL;
using SistemaAcademicoBO.Entities;
using SistemaAcademicoBLL.Util;

namespace SistemaAcademico.Pages
{
    public partial class NewDegreeCourse : System.Web.UI.Page
    {
        #region FIELDS
        ApplicationFacade facade = new ApplicationFacade();
        DegreeCourse degreeCourse;
        int degreeCourseId;

        #endregion

        #region PAGE EVENTS
        protected void Page_Load(object sender, EventArgs e)
        {
            #region REQUEST QUERY STRING
            String action = Request.QueryString["act"];
            if (action == "edit")
            {
                degreeCourseId = Convert.ToInt32(Request.QueryString["id"]);
                degreeCourse = facade.GetDegreeCourse(degreeCourseId);
                txtDegreeCourseName.Text = degreeCourse.Nome;
                txtSemestersQuantity.Text = degreeCourse.SemestersQuantity.ToString();
                ddlCoordenator.SelectedValue = degreeCourse.CoordinatorDegreeCourseId.ToString();

                #region LOAD SUBJECTS IN DEGREE AND SUBJECTS
                if (!IsPostBack)
                {
                    LoadSubjectsInDegreeAndSubjects();
                }
                #endregion
            }
            else
            {
                degreeCourseId = 0;
            }
            if (!IsPostBack)
            {
                UsefullStuffs.LoadSubjects(lstSubjects);

                ddlProfessor.Items.Clear();
                ddlProfessor.Items.Add("Selecione o professor...");
            }
            #endregion

            #region LOAD PROFESSORS
            if (!IsPostBack)
            {
                UsefullStuffs.LoadProfessors(ddlCoordenator);
                ddlCoordenator.Items.Insert(0, "Selecione um coordenador");
            }
            #endregion
        }
        #endregion

        #region PAGE CONTROLS
        protected void btnCadastrar_Click(object sender, EventArgs e)
        {
            if (Validacao())
            {
                if (lstAddedSubjects.Items.Count == 0)
                {
                    Alert.Show("Selecione ao menos uma matéria para o curso.");
                }
                else
                {
                    degreeCourse = new DegreeCourse(degreeCourseId, txtDegreeCourseName.Text, Convert.ToInt32(ddlCoordenator.SelectedValue), Convert.ToInt32(txtSemestersQuantity.Text));
                    facade.SetDegreeCourse(degreeCourse);


                    int identCurrent = facade.GetIdentCurrent("DegreeCourses");
                    if (degreeCourseId == 0)
                    {
                        foreach (ListItem listItem in lstAddedSubjects.Items)
                        {
                            SubjectInDegreeCourse subjectInDegreeCourse = new SubjectInDegreeCourse();
                            subjectInDegreeCourse.DegreeCourseId = identCurrent;

                            string[] values = listItem.Value.Split('-');
                            Subject subject = facade.GetSubject(Convert.ToInt32(values[0].Trim()));

                            subjectInDegreeCourse.ProfessorId = Convert.ToInt32(values[1]);
                            subjectInDegreeCourse.SubjectId = subject.SubjectId;
                            subjectInDegreeCourse.SubjectInDegreeCourseId = 0;
                            facade.SetSubjectInDegreeCourse(subjectInDegreeCourse);
                        }
                    }
                    Alert.Show("Cadastro realizado com sucesso!");
                }
            }
        }

        protected void lbtnAddSubject_Click(object sender, EventArgs e)
        {
            if (ddlProfessor.SelectedIndex > 0)
            {
                if (degreeCourseId != 0)
                {
                    SubjectInDegreeCourse subjectInDegreeCourse = new SubjectInDegreeCourse();
                    subjectInDegreeCourse.DegreeCourseId = degreeCourseId;
                    subjectInDegreeCourse.ProfessorId = Convert.ToInt32(ddlProfessor.SelectedValue);
                    subjectInDegreeCourse.SubjectId = Convert.ToInt32(lstSubjects.SelectedValue);
                    subjectInDegreeCourse.SubjectInDegreeCourseId = 0;
                    facade.SetSubjectInDegreeCourse(subjectInDegreeCourse);

                    UsefullStuffs.LoadAllSubjectsInDegreeCourseByDegreeCourse(lstAddedSubjects, degreeCourseId);

                    lstSubjects.Items.Remove(lstSubjects.Items.FindByValue(subjectInDegreeCourse.SubjectId.ToString()));
                }
                else
                {
                    UsefullStuffs.TransferListItemDegreeCourse(ref lstSubjects, ref lstAddedSubjects, Convert.ToInt32(ddlProfessor.SelectedValue));

                }
            }
            else
            {
                Alert.Show("Selecione o professor.");
            }
        }

        protected void lbtnRemoveSubject_Click(object sender, EventArgs e)
        {
            if (degreeCourseId != 0)
            {
                //ARRUMAR ESSA PARTE! OBS: TALVEZ PROFESSORES SAIA DAQUI!
                //List<SubjectInDegreeCourse> subjectInDegreeCourse = facade.GetAllSubjectInDegreeCourses(degreeCourseId);

            }
            else
            {
                UsefullStuffs.RemoveListItemDegreeCourse(ref lstAddedSubjects, ref lstSubjects);
            }
        }

        protected void lstSubjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlProfessor.Items.Clear();
            ddlProfessor.Items.Add("Selecione o professor...");
            List<ProfessorSubject> professorSubjects = facade.GetAllProfessorsSubjectsBySubjectId(Convert.ToInt32(lstSubjects.SelectedValue));
            foreach (ProfessorSubject professorSubject in professorSubjects)
            {
                Professor professor = facade.GetProfessor(professorSubject.ProfessorId);
                ddlProfessor.Items.Add(new ListItem(professor.Nome, professor.ProfessorId.ToString()));
            }
            lstAddedSubjects.SelectedIndex = -1;
        }

        protected void lstAddedSubjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstSubjects.SelectedIndex = -1;
        }

        #endregion

        #region METHODS
        private bool Validacao()
        {
            if (string.IsNullOrEmpty(txtDegreeCourseName.Text))
            {
                Alert.Show("Digite o nome do curso superior.");
                return false;
            }
            if (string.IsNullOrEmpty(txtSemestersQuantity.Text))
            {
                Alert.Show("Digite a quantidade de semestres.");
                return false;
            }
            if (ddlCoordenator.SelectedIndex == 0)
            {
                Alert.Show("Escolha o coordenador do curso.");
                return false;
            }
            if (lstAddedSubjects.Items.Count == 0)
            {
                Alert.Show("Selecione ao menos uma matéria.");
                return false;
            }
            return true;
        }

        private void LoadSubjectsInDegreeAndSubjects()
        {
            UsefullStuffs.LoadAllSubjectsInDegreeCourseByDegreeCourse(lstAddedSubjects, degreeCourseId);
            UsefullStuffs.LoadSubjects(lstSubjects);
            foreach (ListItem listItem in lstAddedSubjects.Items)
            {
                lstSubjects.Items.Remove(lstSubjects.Items.FindByValue(listItem.Value));
            }
        }
        #endregion










    }
}